package com.atwei.mango.mybatis;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.executor.resultset.ResultSetHandler;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.Properties;

/**
 * @Classname MybatisPluginsLearn
 * @Description mybatis插件学习
 * @Date 2022/5/11 14:57
 * @Author liuwei
 */
//@Component
@Intercepts({
        @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}),
        @Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class}),
        @Signature(type = ParameterHandler.class, method = "setParameters", args = {PreparedStatement.class}),
        @Signature(type = ResultSetHandler.class, method = "handleResultSets", args = {Statement.class})
})
public class MybatisPluginsLearn implements Interceptor {
    @Override
    public Object intercept(Invocation invocation) throws Throwable{
        return invocation.proceed();
    }

    @Override
    public Object plugin(Object target) {
//        //只对要拦截的对象生成代理
//        if(target instanceof StatementHandler){
//            //调用插件
//            return Plugin.wrap(target, this);
//        }
//        return target;
        return Interceptor.super.plugin(target);
    }
    @Override
    public void setProperties(Properties properties) {
        Interceptor.super.setProperties(properties);
    }
}
